package com.ruoyi.house_sale.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.house_sale.domain.CustomerInfo;
import com.ruoyi.house_sale.service.ICustomerInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.house_sale.domain.CustEvent;
import com.ruoyi.house_sale.service.ICustEventService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客户事件Controller
 * 
 * @author ruoyi
 * @date 2022-04-09
 */
@RestController
@RequestMapping("/house_sale/CustEvent")
public class CustEventController extends BaseController
{
    @Autowired
    private ICustEventService custEventService;
    @Autowired
    private ICustomerInfoService customerInfoService;

    /**
     * 查询客户事件列表
     */
    @PreAuthorize("@ss.hasPermi('house_sale:CustEvent:list')")
    @GetMapping("/list")
    public TableDataInfo list(CustEvent custEvent)
    {
        startPage();
        List<CustEvent> list = custEventService.selectCustEventList(custEvent);
        return getDataTable(list);
    }

    /**
     * 导出客户事件列表
     */
    @PreAuthorize("@ss.hasPermi('house_sale:CustEvent:export')")
    @Log(title = "客户事件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CustEvent custEvent)
    {
        List<CustEvent> list = custEventService.selectCustEventList(custEvent);
        ExcelUtil<CustEvent> util = new ExcelUtil<CustEvent>(CustEvent.class);
        util.exportExcel(response, list, "客户事件数据");
    }

    /**
     * 获取客户事件详细信息
     */
    @PreAuthorize("@ss.hasPermi('house_sale:CustEvent:query')")
    @GetMapping(value = "/{eventid}")
    public AjaxResult getInfo(@PathVariable("eventid") Long eventid)
    {
        return AjaxResult.success(custEventService.selectCustEventByEventid(eventid));
    }

    /**
     * 新增客户事件
     */
    @PreAuthorize("@ss.hasPermi('house_sale:CustEvent:add')")
    @Log(title = "客户事件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustEvent custEvent)
    {
        custEventService.updateCustEvent(custEvent);
        CustomerInfo  customerInfo=customerInfoService.selectCustomerInfoByCustid(custEvent.getCustid());
        customerInfo.setEventtypeid(custEvent.getEventtypeid());
        customerInfoService.updateCustomerInfo(customerInfo);

        return toAjax(custEventService.insertCustEvent(custEvent));
    }

    /**
     * 修改客户事件
     */
    @PreAuthorize("@ss.hasPermi('house_sale:CustEvent:edit')")
    @Log(title = "客户事件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustEvent custEvent)
    {
        custEventService.updateCustEvent(custEvent);
        CustomerInfo  customerInfo=customerInfoService.selectCustomerInfoByCustid(custEvent.getCustid());
        customerInfo.setEventtypeid(custEvent.getEventtypeid());
        customerInfoService.updateCustomerInfo(customerInfo);

        return toAjax(custEventService.updateCustEvent(custEvent));
    }

    /**
     * 删除客户事件
     */
    @PreAuthorize("@ss.hasPermi('house_sale:CustEvent:remove')")
    @Log(title = "客户事件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{eventids}")
    public AjaxResult remove(@PathVariable Long[] eventids)
    {
        return toAjax(custEventService.deleteCustEventByEventids(eventids));
    }

    /**
     * 根据客户id查询事件信息
     */
    @PreAuthorize("@ss.hasPermi('house_sale:CustEvent:eventlist')")
    @Log(title = "客户事件", businessType = BusinessType.DELETE)
    @DeleteMapping(value="/{custId}")
    public AjaxResult list(@PathVariable("custId") Long  custId)
    {
        return AjaxResult.success(custEventService.selectEventList(custId));
    }
}
